using System;
using UnityEngine;

namespace CGF
{
    public partial class Dialog : UIComponentBase, IAwake<UILayer, bool>
    {
        private const int MaxTipLength = 200;

        public static bool IsVisible
        {
            get { return GameLaunch.Instance.HasComponent<Dialog>(); }
        }

        public static async CTask Alert(string content, Action onClose = null)
        {
            Dialog alert = UIManagerComponent.instance.OpenUI<Dialog>(UILayer.Top, GameLaunch.Instance, true);
            await alert.SetAlertContent(content, onClose);
        }

        public static async CTask<bool> Confirm(string content, Action onOK = null, Action onNo = null, string yesBtnText = null, string noBtnText = null)
        {
            Dialog alert = UIManagerComponent.instance.OpenUI<Dialog>(UILayer.Top, GameLaunch.Instance, true);
            return await alert.SetConfirmContent(content, onOK, onNo, yesBtnText, noBtnText);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<Dialog>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/Common/Dialog", layer, isFloat);
            BindProperties(transform);
            ScaleIn(Popup.transform);
        }

        private async CTask SetAlertContent(string content, Action onClose)
        {
            for (int i = 0; i < 100; i++)
            {
                if (Content) break;
                //等待Awake执行结束
                await CTask.Wait(0.01f);
            }
            CTask task = CTask.Create();
            Content.text = content != null && content.Length > MaxTipLength
                ? content.Substring(0, MaxTipLength)
                : content;
            BtnNo.gameObject.SetActive(false);
            BtnYes.SetClick(() =>
            {
                Close();
                onClose?.Invoke();
                task.SetResult();
            });
            await task;
        }

        private async CTask<bool> SetConfirmContent(string content, Action onOK, Action onNo, string yesBtnText, string noBtnText)
        {
            for (int i = 0; i < 100; i++)
            {
                if (Content) break;
                //等待Awake执行结束
                await CTask.Wait(0.01f);
            }
            CTask<bool> task = CTask<bool>.Create();
            Content.text = content.Length > MaxTipLength ? content.Substring(0, MaxTipLength) : content;
            BtnNo.gameObject.SetActive(true);
            if (!string.IsNullOrEmpty(yesBtnText))
            {
                BtnYes.text.text = yesBtnText;
            }
            if (!string.IsNullOrEmpty(noBtnText))
            {
                BtnNo.text.text = noBtnText;
            }
            BtnYes.SetClick(() =>
            {
                Close();
                onOK?.Invoke();
                task.SetResult(true);
            });
            BtnNo.SetClick(() =>
            {
                Close();
                onNo?.Invoke();
                task.SetResult(false);
            });
            return await task;
        }
    }
}